import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    bannerType: '',
    bannerData: '',
    newsType: '',
    newsData: '',
    currentIndex: '',
    newsSelect: '',
    columnId: '',
    columnParent: '',
    cooperativeId: '',
    industryId: '',
    industry: '',
    branchId: "",
    branchVal: "",
  },
  mutations: {
    setBannerType (state, type) {
      state.bannerType = type
      window.localStorage.setItem('bannerType', type)
    },
    setBannerData (state, data) {
      state.bannerData = data
      window.localStorage.setItem('bannerData', JSON.stringify(data))
    },
    setNewsType (state, type) {
      state.newsType = type
      window.localStorage.setItem('newsType', type)
    },
    setNewsData (state, data) {
      state.newsData = data
      window.localStorage.setItem('newsData', JSON.stringify(data))
    },
    setCurrentIndex (state, data) {
      state.currentIndex = data
      window.localStorage.setItem('currentIndex', JSON.stringify(data))
    },
    setNewsSelect (state, selectArr) {
      state.newsSelect = selectArr
      window.localStorage.setItem('newsSelect', JSON.stringify(selectArr))
    },
    setColumnId (state, payload) {
      state.columnId = payload;
      window.localStorage.setItem("columnId", payload);
    },
    setColumnParent (state, payload) {
      state.columnParent = payload;
      window.localStorage.setItem("columnParent", payload)
    },
    setCooperativeId (state, payload) {
      state.cooperativeId = payload
      window.localStorage.setItem("cooperativeId", payload)
    },
    setIndustryId (state, payload) {
      state.industryId = payload
      window.localStorage.setItem("industryId", payload)
    },
    setIndustry (state, payload) {
      state.industry = payload
      window.localStorage.setItem("industry", JSON.stringify(payload))
    },
    setBranchId (state, payload) {
      state.branchId = payload;
      window.localStorage.setItem("branchId", payload)
    },
    setBranchVal (state, payload) {
      state.branchVal = payload;
      window.localStorage.setItem("branchVal", JSON.stringify(payload))
    },
    setMediaId (state, payload) {
      state.branchId = payload;
      window.localStorage.setItem("mediaId", payload)
    },
    setMediaVal (state, payload) {
      state.branchVal = payload;
      window.localStorage.setItem("mediaVal", JSON.stringify(payload))
    },
    setToken (state, payload) {
      state.token = payload;
      window.localStorage.setItem("myToken", payload);
    },
    setUsername (state, payload) {
      state.userName = payload;
      window.localStorage.setItem("userName", payload);
    },
    clearToken (state) {
      window.localStorage.removeItem("myToken");
    }
  },
  actions: {

  },
  getters: {
    bannerType (state) {
      return state.bannerType ? state.bannerType : window.localStorage.getItem('bannerType')
    },
    bannerData (state) {
      return state.bannerData ? state.bannerData : JSON.parse(window.localStorage.getItem('bannerData'))
    },
    newsType (state) {
      return state.newsType ? state.newsType : window.localStorage.getItem('newsType')
    },
    newsData (state) {
      return state.newsData ? state.newsData : JSON.parse(window.localStorage.getItem('newsData'))
    },
    getCurrentIndex (state) {
      const index = JSON.parse(window.localStorage.getItem('currentIndex'))
      return state.currentIndex ? state.currentIndex : index ? index : '1-1'
    },
    getNewsSelect (state) {
      return state.newsSelect ? state.newsSelect : JSON.parse(window.localStorage.getItem('newsSelect'))
    },
    cooperativeId (state) {
      return state.cooperativeId ? state.cooperativeId : window.localStorage.getItem('cooperativeId')
    },
    industryId (state) {
      return state.industryId ? state.industryId : window.localStorage.getItem('industryId')
    },
    industry (state) {
      return state.industry ? state.industry : JSON.parse(window.localStorage.getItem('industry'))
    },
    branchId (state) {
      return state.branchId ? state.branchId : window.localStorage.getItem('branchId')
    },
    branchVal (state) {
      return state.branchVal ? state.branchVal : JSON.parse(window.localStorage.getItem('branchVal'))
    },
    mediaId (state) {
      return state.mediaId ? state.mediaId : window.localStorage.getItem('mediaId')
    },
    mediaVal (state) {
      return state.mediaVal ? state.mediaVal : JSON.parse(window.localStorage.getItem('mediaVal'))
    },
    getToken (state) {
      if (state.token) {
        return state.token;
      }
      return localStorage.getItem("myToken");
    },
    getUsername (state) {
      if (state.userName) {
        return state.username;
      }
      return localStorage.getItem("userName");
    },
  },
  modules: {}
})
